import com.wang.pojo.Customer;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;

public class Mytest {
   @Test
    public void findCustomerByIdTest()throws Exception{
       String resource="mybatis-config.xml";
       InputStream inputStream=Resources.getResourceAsStream(resource);
       SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
       SqlSession sqlSession=sqlSessionFactory.openSession();
       Customer customer=sqlSession.selectOne("com.wang.mapper"+".CustomerMapper.findCustomerById",1);
       System.out.println(customer.toString());
       sqlSession.close();
    }
    @Test
    public void addCustomerTest() throws Exception{
        //读取配置文件
        String resource="mybatis-config.xml";
        InputStream inputStream=Resources.getResourceAsStream(resource);
        //根据配置文件构建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        //通过SqlSessionFactory来创建SqlSession
        SqlSession sqlSession=sqlSessionFactory.openSession();
        //创建Customer对象，并向对象中添加数据
        Customer customer=new Customer();
        customer.setUsername("rose");
        customer.setPhone("12345678912");
        customer.setJobs("students");
        //执行SqlSession的插入方法，返回的是SQL语句影响的行数
        int rows=sqlSession.insert("com.wang.mapper"+".CustomerMapper.addCustomer",customer);
        if (rows>0){
            System.out.println("您成功插入"+rows+"条数据");
        }else {
            System.out.println("执行操作失败");
        }
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void updateCustomerTest() throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Customer customer = new Customer();
        customer.setId(4);
        customer.setJobs("programmer");
        customer.setUsername("rose");
        customer.setPhone("1234578988");
        int rows = sqlSession.update("com.wang.mapper" + ".CustomerMapper.updateCustomer", customer);
        if (rows > 0) {
            System.out.println("您成功修改" + rows + "条数据");
        } else {
            System.out.println("执行操作失败");
        }
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void deleteCustomerTest()throws Exception{
       String resource="mybatis-config.xml";
       InputStream inputStream=Resources.getResourceAsStream(resource);
       SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
       SqlSession sqlSession=sqlSessionFactory.openSession();
       int rows=sqlSession.delete("com.wang.mapper"+".CustomerMapper.deleteCustomer",3);
       if (rows>0){
           System.out.println("您成功删除了"+rows+"条数据");
       }else {
           System.out.println("执行删除操作失败");
       }
       sqlSession.commit();
       sqlSession.close();
    }
}